package com.hemj.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hemj.common.exception.ServiceException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hemj.system.mapper.SysRegisterMapper;
import com.hemj.system.domain.SysRegister;
import com.hemj.system.service.ISysRegisterService;
import com.hemj.common.utils.IdGenerator.IdGeneratorFactory;
import com.hemj.system.vo.SysRegisterVo;
import com.hemj.system.bo.SysRegisterBo;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统注册Service业务层处理
 * 
 * @author hemj
 * @date 2024-10-21
 */
@Service
public class SysRegisterServiceImpl extends ServiceImpl<SysRegisterMapper, SysRegister> implements ISysRegisterService
{
    @Autowired
    private IdGeneratorFactory idGeneratorFactory;
    
    /**
         * 验证参数
         */
    private void validateData(SysRegister sysRegister){
    }

    /**
     * 保存数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public SysRegister saveCascade(SysRegisterVo jsonObject){
        SysRegister sysRegister = new SysRegister();
        BeanUtils.copyProperties(jsonObject,sysRegister);
        validateData(sysRegister);
        sysRegister.setId(idGeneratorFactory.getId(SysRegister.class.getSimpleName()));
        this.save(sysRegister);
        return sysRegister;
    }
    /**
     * 编辑数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public SysRegister updateCascade(SysRegisterVo jsonObject){
        SysRegister sysRegister = this.getById(jsonObject.getId());;
        BeanUtils.copyProperties(jsonObject,sysRegister);
        validateData(sysRegister);
        this.updateById(sysRegister);
        return sysRegister;
    }

    /**
     * 初始化查询条件
     */
    public LambdaQueryWrapper<SysRegister> initQueryWrapper(SysRegisterVo jsonObject){
        LambdaQueryWrapper<SysRegister> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(jsonObject.getId())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getName())) {
            queryWrapper.like(SysRegister::getName, jsonObject.getName());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getCode())) {
            queryWrapper.eq(SysRegister::getCode, jsonObject.getCode());
        }
        if (!ObjectUtils.isEmpty(jsonObject.getCreateBy())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getCreateTime())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getUpdateBy())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getUpdateTime())) {
        }
        if (!ObjectUtils.isEmpty(jsonObject.getStatus())) {
            queryWrapper.eq(SysRegister::getStatus, jsonObject.getStatus());
        }
        queryWrapper.orderByDesc(SysRegister::getId);
        return queryWrapper;
    }

    /**
     * list查询
     */
    public List<SysRegisterBo> queryList(SysRegisterVo jsonObject) {
        List<SysRegister> sysRegisters = this.list(initQueryWrapper(jsonObject));
        List<SysRegisterBo> bos = new ArrayList<>();
        for (SysRegister sysRegister:sysRegisters) {
            SysRegisterBo sysRegisterBo = new SysRegisterBo();
            BeanUtils.copyProperties(sysRegister,sysRegisterBo);
            bos.add(sysRegisterBo);
        }
        return bos;
    }

    /**
     * 统计
     */
    public long countList(SysRegisterVo jsonObject) {
        return this.count(initQueryWrapper(jsonObject));
    }

    /**
     * 获取单个数据
     */
    public SysRegisterBo getOneById(Long id){
        SysRegister sysRegister = this.getById(id);
        SysRegisterBo sysRegisterBo = new SysRegisterBo();
        BeanUtils.copyProperties(sysRegister,sysRegisterBo);
        return sysRegisterBo;
    }

}
